package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.MarketSettlementPlanResult
import com.zxy.supplier_system.server.service.MarketSettlementPlanResultService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal

@RestController
@RequestMapping("markets/{marketId}/settlementPlans/results")
class MarketSettlementPlanResultController(
    private val marketSettlementPlanResultService: MarketSettlementPlanResultService,
) {

    @GetMapping("year/{year}/month/{month}/previewMonthly")
    fun previewMonthlyResult(
        @PathVariable marketId: Int,
        @PathVariable year: Int,
        @PathVariable month: Int
    ): PreviewMonthlySettlementResultsResponse {
        return this.marketSettlementPlanResultService.previewMonthlyResult(marketId, year, month)
    }

    /**
     * 应结算金额=总销量-总费用
     */
    data class PreviewMonthlySettlementResultsResponse(
        /**
         * 总费用
         */
        val totalFee: BigDecimal,
        /**
         * 总销量
         */
        val totalSale: BigDecimal,
        val ruleFeeList: List<RuleFee>
    ) {

        data class RuleFee(
            val id: Int,
            val name: String,
            val note: String?,
            /**
             * 费用计算结果
             */
            val fee: BigDecimal,
        )

    }

    @GetMapping("year/{year}/month/{month}")
    fun getDocuments(
        @PathVariable marketId: Int,
        @PathVariable year: Int,
        @PathVariable month: Int
    ): MarketSettlementPlanResultResponse {
        return this.marketSettlementPlanResultService.getDocuments(marketId, year, month)
    }

    data class MarketSettlementPlanResultResponse(
        val totalSale: BigDecimal,
        val feeList: List<PreviewMonthlySettlementResultsResponse.RuleFee>,
    )

    @PostMapping("year/{year}/month/{month}/generateMonthly")
    fun generateMonthlyResult(@PathVariable marketId: Int, @PathVariable year: Int, @PathVariable month: Int) {
        return this.marketSettlementPlanResultService.generateMonthlyResult(
            marketId,
            year,
            month
        )
    }

    @GetMapping("year/{year}/month/{month}/plan")
    fun getMonthlyCopiedPlan(
        @PathVariable marketId: Int,
        @PathVariable year: Int,
        @PathVariable month: Int
    ): MonthlySettlementPlanResultPlanResponse {
        return this.marketSettlementPlanResultService.getMonthlyCopiedPlan(marketId, year, month)
    }

    data class MonthlySettlementPlanResultPlanResponse(
        val rules: List<MarketSettlementPlanResult.MarketSettlementRuleData>,
    )


    @GetMapping
    fun getResultSummaries(@PathVariable marketId: Int, pageable: Pageable): Page<MarketSettlementPlanResultSummary> {
        return this.marketSettlementPlanResultService.findResultSummaries(marketId, pageable)
    }

    data class MarketSettlementPlanResultSummary(
        val year: Int,
        val month: Int,
        val totalFee: BigDecimal,
    )
}